package com.gusha.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysAssetsDao;
import com.gusha.model.ReceivingAddress;
import com.gusha.model.SysAssets;
import com.gusha.model.SysOrder;
import com.gusha.model.SysShopping;
import com.gusha.model.User;

@Service
@Transactional
public class SysAssetsService {
	@Autowired
	SysAssetsDao sysAssetsDao;
	
	
	public Page<SysAssets> findAll(Pageable pageable, SysAssets sysAssets) {
        @SuppressWarnings("serial")
        Page<SysAssets> result = sysAssetsDao.findAll(new Specification<SysAssets>() {
            @Override
            public Predicate toPredicate(Root<SysAssets> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysAssets.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysAssets.getId()));
                }
                if (sysAssets.getUserId() != null) {
                    list.add(cb.equal(root.get("userId").as(User.class),sysAssets.getUserId()));
                }
                if (sysAssets.getShoppingNum() != null) {
                    list.add(cb.equal(root.get("shoppingNum").as(Long.class),sysAssets.getShoppingNum()));
                }
                if (sysAssets.getStealNum() != null) {
                    list.add(cb.equal(root.get("stealNum").as(Long.class),sysAssets.getStealNum()));
                }
                if (sysAssets.getPlantNum() != null) {
                    list.add(cb.equal(root.get("plantNum").as(Long.class),sysAssets.getPlantNum()));
                }
                if (sysAssets.getFinishedNum() != null) {
                    list.add(cb.equal(root.get("finishedNum").as(Long.class),sysAssets.getFinishedNum()));
                }
                if (sysAssets.getShoppingType() != null) {
                    list.add(cb.equal(root.get("shoppingType").as(BigInteger.class),sysAssets.getShoppingType()));
                }
                if (sysAssets.getPayTime() != null) {
                    list.add(cb.equal(root.get("payTime").as(Date.class),sysAssets.getPayTime()));
                }
                if (sysAssets.getPlantTime() != null) {
                    list.add(cb.equal(root.get("plantTime").as(Date.class),sysAssets.getPlantTime()));
                }
                if (sysAssets.getFinishedTime() != null) {
                    list.add(cb.equal(root.get("finishedTime").as(Date.class),sysAssets.getFinishedTime()));
                }
                if (sysAssets.getShoppingName() != null) {
                    list.add(cb.equal(root.get("shoppingName").as(String.class),sysAssets.getShoppingName()));
                }
                if (sysAssets.getShoppingImg() != null) {
                    list.add(cb.equal(root.get("shoppingImg").as(String.class),sysAssets.getShoppingImg()));
                }
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
